package in.co.codedoc.db;

import java.sql.Connection;
import java.sql.DriverManager;

import in.co.codedoc.cg.annotations.HasProperties;
import in.co.codedoc.cg.annotations.IsAnIOCComponent;
import in.co.codedoc.cg.annotations.IsMappedToJSON;
import in.co.codedoc.cg.annotations.Property;
import in.co.codedoc.config.Configurable;
import in.co.codedoc.config.ConfigurationUtil;
import in.co.codedoc.config.Configurator;
import in.co.codedoc.json.JSONValue;
import in.co.codedoc.sql.DBConnectionProvider;

@IsAnIOCComponent
@IsMappedToJSON
@HasProperties
({
	@Property(name="database",datatype=String.class),
	@Property(name="user",datatype=String.class),
	@Property(name="password",datatype=String.class)
})
public class DBConnectionProviderImpl
	extends DBConnectionProviderImplBase
	implements Configurable,DBConnectionProvider
{
	DBConnectionProviderImpl()
	{
		
	}
	
	public DBConnectionProviderImpl(Configurator configurator)
	{
		
	}
	
	@Override
	public void Configure(JSONValue jsonData)
	{
		ConfigurationUtil.ConfigureUsingProperties(this, jsonData);
	}

	@Override
	public String GetConfigSectionName()
	{
		return "DB";
	}

	@Override
	public Connection GetConnection()
	{
		try
		{
			Connection connection 
				= DriverManager.getConnection(GetDatabase(),GetUser(),GetPassword());
			
			connection.setAutoCommit(false);
			
			return connection;
		}
		catch (Throwable th)
		{
			throw new RuntimeException(th);
		}
	}

}
